# -*- coding: utf-8 -*-
import threading
import socket
HOST=raw_input("Remote Address: ");
PORT=3029
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)      
s.connect((HOST,PORT))       

class GetBack (threading.Thread):

	def __init__(self,socket,callback):
		threading.Thread.__init__(self)
		self.soc=socket
		self.cbk=callback

	def run(self):
		while True :
			rcv=''
			while True:
				buf = self.soc.recv(1024)
				if (ord(buf[-1]) == 0xFF ):
					rcv =rcv + buf[0:-1]
					self.cbk(rcv)
					break
				else :
					rcv =rcv + buf;

class sendMsg(threading.Thread):
	def __init__(self,socket,back):
		threading.Thread.__init__(self)
		self.soc=socket
		self.back=back

	def run(self):
		gs.start()
		while True :
			txt=raw_input("请输入要发送的话:")
			if (txt == "!quit"):
				self.soc.close()
				break
			self.soc.sendall(txt)
def show(str):
	print "Server Say: "+str
gs=GetBack(s,show)
sd=sendMsg(s,gs)

sd.start()
#s.sendall('hhhhhh')
#while True:
#	rcv=s.recv(1024)     
#	if (rcv != ""):
#		print rcv
#	print "服务器说："+rcv  
#	txt=raw_input("请输入要发送的话:")
#	if (txt == "!quit"):
#		s.close()
#		break
#	s.sendall(txt)
